
package com.scs.application.modules.settlement.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.scs.application.core.exceptions.BusinessException;
import com.scs.application.core.model.CommonResult;
import com.scs.application.core.model.PageResult;
import com.scs.application.core.model.RestResult;
import com.scs.application.core.mvc.controller.CrudController;
import com.scs.application.core.mvc.controller.ImAndExAbleController;
import com.scs.application.core.pdf.PdfExportUtils;
import com.scs.application.core.pdf.entity.PdfExportParams;
import com.scs.application.core.pdf.enums.PdfPageDirection;
import com.scs.application.core.pdf.enums.SpacingMode;
import com.scs.application.core.utils.DateUtils;
import com.scs.application.core.utils.FileUtils;
import com.scs.application.core.utils.FreeMarkers;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.enums.BasePrintBusType;
import com.scs.application.modules.base.entity.Hospital;
import com.scs.application.modules.base.entity.Supplier;
import com.scs.application.modules.base.request.PrintTemplateQueryRequest;
import com.scs.application.modules.base.service.HospitalService;
import com.scs.application.modules.base.service.PrintTemplateService;
import com.scs.application.modules.base.service.SupplierService;
import com.scs.application.modules.base.vo.PrintTemplateVO;
import com.scs.application.modules.design.service.QueryGenerator;
import com.scs.application.modules.settlement.consts.InvoiceGroupType;
import com.scs.application.modules.settlement.dto.InvoiceBuildDTO;
import com.scs.application.modules.settlement.dto.InvoiceDTO;
import com.scs.application.modules.settlement.entity.Invoice;
import com.scs.application.modules.settlement.request.InvoiceItemQueryRequest;
import com.scs.application.modules.settlement.request.InvoicePdfReviewRequest;
import com.scs.application.modules.settlement.request.InvoiceQueryRequest;
import com.scs.application.modules.settlement.service.InvoiceItemService;
import com.scs.application.modules.settlement.service.InvoiceService;
import com.scs.application.modules.settlement.vo.InvoiceItemVO;
import lombok.AllArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.Serializable;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 开票单 controller
 */
@AllArgsConstructor
@RestController
@RequestMapping("/settlement/invoices")
public class InvoiceController extends CrudController<Invoice, String, InvoiceService> implements ImAndExAbleController<InvoiceQueryRequest> {

    @Autowired
    private InvoiceItemService invoiceItemService;

    @Autowired
    private PrintTemplateService printTemplateService;

    @Autowired
    private HospitalService hospitalService;

    @Autowired
    private SupplierService supplierService;

    @PostMapping
    public CommonResult add(@RequestBody @Valid Invoice entity) {
        boolean bSave = service.add(entity);
        return bSave ? CommonResult.ok("新增成功", entity) : CommonResult.error("新增失败");
    }

    @GetMapping("/page")
    public PageResult page(InvoiceQueryRequest request) {
        IPage<Invoice> page = service.page(request);

        //填充开票信息
        if (page.getTotal() > 0) {
            List<String> spidList = page.getRecords().stream().map(Invoice::getSupplierId).distinct().collect(Collectors.toList());
            List<String> hosidList = page.getRecords().stream().map(Invoice::getHospitalId).distinct().collect(Collectors.toList());

            Map<String,Supplier> spById = supplierService.list(Wrappers.<Supplier>query().in("id",spidList)).stream().collect(Collectors.toMap(Supplier::getId, item -> item));
            Map<String, Hospital> hosById = hospitalService.list(Wrappers.<Hospital>query().in("id",hosidList)).stream().collect(Collectors.toMap(Hospital::getId, item -> item));
            BusinessException.throwErrorIf(ObjectUtil.length(spidList) != ObjectUtil.length(spById.keySet()), "供应商信息缺失，请检查");
            BusinessException.throwErrorIf(ObjectUtil.length(hosidList) != ObjectUtil.length(hosById.keySet()), "医院信息缺失，请检查");
            for(Invoice invoice : page.getRecords()) {
                if (StringUtils.isNotBlank(invoice.getSupplierId())) {
                    invoice.setSpInvoiceName(spById.get(invoice.getSupplierId()).getInvoiceName());
                    invoice.setSpInvoiceNo(spById.get(invoice.getSupplierId()).getInvoiceNo());
                    invoice.setSpInvoiceAddress(spById.get(invoice.getSupplierId()).getInvoiceAddress());
                    invoice.setSpInvoiceBankName(spById.get(invoice.getSupplierId()).getInvoiceBankName());
                    invoice.setSpInvoiceBankNo(spById.get(invoice.getSupplierId()).getInvoiceBankNo());
                    invoice.setSpInvoiceTel(spById.get(invoice.getSupplierId()).getInvoiceTel());
                }
                if (StringUtils.isNotBlank(invoice.getHospitalId())) {
                    invoice.setHosInvoiceName(hosById.get(invoice.getHospitalId()).getInvoiceName());
                    invoice.setHosInvoiceNo(hosById.get(invoice.getHospitalId()).getInvoiceNo());
                    invoice.setHosInvoiceAddress(hosById.get(invoice.getHospitalId()).getInvoiceAddress());
                    invoice.setHosInvoiceBankName(hosById.get(invoice.getHospitalId()).getInvoiceBankName());
                    invoice.setHosInvoiceBankNo(hosById.get(invoice.getHospitalId()).getInvoiceBankNo());
                    invoice.setHosInvoiceTel(hosById.get(invoice.getHospitalId()).getInvoiceTel());
                }
            }
        }
        return PageResult.ok("查询成功", page.getRecords(), page.getTotal());
    }

    @GetMapping("/list")
    public CommonResult list(InvoiceQueryRequest request) {
        List<Invoice> list = service.list(request);
        return CommonResult.ok("查询成功", list);
    }

    /**
     * 审核
     */
    @PostMapping("/audit")
    public CommonResult audit(@RequestBody Invoice invoice) {
        service.audit(invoice);
        return CommonResult.ok("审核成功");
    }

    /**
     * 保存开票单
     */
    @PostMapping("/save_invoice")
    public CommonResult saveInvoice(@RequestBody @Valid InvoiceDTO invoiceDTO) {
        Double invoiceAmount = service.saveInvoice(invoiceDTO);
        return CommonResult.ok("开票成功", invoiceAmount);
    }

    /**撤销*/
    @PostMapping("/cancel")
    public CommonResult cancel(@RequestBody Invoice invoice) {
        service.cancel(invoice);
        invoice = service.getById(invoice.getId());
        return CommonResult.ok("撤销成功", invoice);
    }

    /**
     * 删除制单状态的开票单
     */
    @DeleteMapping("/delete_invoice/{id}")
    public CommonResult deleteInvoiceById(@PathVariable("id") String id) {
        boolean bRemove = service.deleteInvoiceById(id);
        return bRemove ? CommonResult.ok("删除成功") : CommonResult.error("删除失败");
    }

    /**
     * 恢复结算单到待审核状态
     */
    @PostMapping("/resume_invoice/{id}")
    public CommonResult resumeInvoice(@PathVariable("id") String id) {
        boolean bResume = service.resumeInvoice(id);
        return bResume ? CommonResult.ok("恢复成功") : CommonResult.error("恢复失败");
    }

    /**
     * PDF打印预览
     *
     * @param id
     * @param response
     */
    @GetMapping("/{id}/pdf-preview")
    public void pdfPreview(@PathVariable(name = "id") String id,
                           @RequestParam(name = "direction", required = false, defaultValue = "transverse") String direction,
                           @RequestParam(name = "spacingMode", required = false, defaultValue = "normal") String spacingMode,
                           @RequestParam(name = "pageSize", required = false, defaultValue = "A4") String pageSize,
                           @RequestParam(name = "perRowsOfGrid", required = false, defaultValue = "5") int perRowsOfGrid,
                           HttpServletResponse response) throws IOException {

        InvoicePdfReviewRequest invoicePdfReviewRequest = new InvoicePdfReviewRequest();
        invoicePdfReviewRequest.setId(id).setDirection(direction).setSpacingMode(spacingMode)
                .setPageSize(pageSize).setPerRowsOfGrid(perRowsOfGrid).setLoadFromTemplate(true)
                .setBasePrintBusType(BasePrintBusType.INVOICE)
        ;

        Invoice byId = this.service.getById(id);

        String templateJson = null;
        PdfPageDirection pageDirection = PdfPageDirection.of(direction);
        SpacingMode sMode = SpacingMode.of(spacingMode);
        //查询改医院是否配置自定义模板
        PrintTemplateVO iv = printTemplateService.findPrintTemplateVO(new PrintTemplateQueryRequest().setHospitalId(byId.getHospitalId())
                .setBusType(BasePrintBusType.INVOICE.getValue()).setThisPageSize(pageSize).setMaterialTypeCode(2));

        if (iv.getId() != null) {
            templateJson = iv.getConfigJson();
        }else{
            templateJson = String.format("print-template/invoice/%s.json", pageSize.toUpperCase());
        }

        invoicePdfReviewRequest.setPrintPath(templateJson);
        pdfPreview(invoicePdfReviewRequest, response);
    }

    /**
     * PDF打印预览 根据入库单号、品名，品规、价格打印
     *
     * @param id
     * @param response
     */
    @GetMapping("/{id}/pdf-preview/hrp_key")
    public void pdfPreviewByHrpKey(@PathVariable(name = "id") String id,
                                   @RequestParam(name = "direction", required = false, defaultValue = "transverse") String direction,
                                   @RequestParam(name = "spacingMode", required = false, defaultValue = "normal") String spacingMode,
                                   @RequestParam(name = "pageSize", required = false, defaultValue = "A4") String pageSize,
                                   @RequestParam(name = "perRowsOfGrid", required = false, defaultValue = "5") int perRowsOfGrid,
                                   HttpServletResponse response) throws IOException {

        InvoicePdfReviewRequest invoicePdfReviewRequest = new InvoicePdfReviewRequest();
        invoicePdfReviewRequest.setId(id).setDirection(direction).setSpacingMode(spacingMode)
                .setPageSize(pageSize).setPerRowsOfGrid(perRowsOfGrid).setLoadFromTemplate(false)
                .setBasePrintBusType(BasePrintBusType.INVOICE)
                .setPrintPath(String.format("print-template/invoice/hrpkey/%s.json", pageSize.toUpperCase()));
        pdfPreview(invoicePdfReviewRequest, response);
    }

    /**
     * PDF打印预览
     *
     * @param id
     * @param response
     */
    @GetMapping("/{id}/pdf-preview/delivery")
    public void pdfPreviewDelivery(@PathVariable(name = "id") String id,
                                   @RequestParam(name = "direction", required = false, defaultValue = "transverse") String direction,
                                   @RequestParam(name = "spacingMode", required = false, defaultValue = "normal") String spacingMode,
                                   @RequestParam(name = "pageSize", required = false, defaultValue = "A4") String pageSize,
                                   @RequestParam(name = "perRowsOfGrid", required = false, defaultValue = "5") int perRowsOfGrid,
                                   HttpServletResponse response) throws IOException {
        InvoicePdfReviewRequest invoicePdfReviewRequest = new InvoicePdfReviewRequest();
        invoicePdfReviewRequest.setId(id).setDirection(direction).setSpacingMode(spacingMode)
                .setPageSize(pageSize).setPerRowsOfGrid(perRowsOfGrid).setLoadFromTemplate(true)
                .setBasePrintBusType(BasePrintBusType.INVOICEDELIVERY)
                .setPrintPath(String.format("print-template/invoice/delivery/%s.json", pageSize.toUpperCase()));
        pdfPreview(invoicePdfReviewRequest, response);
    }

    private void pdfPreview(InvoicePdfReviewRequest invoicePdfReviewRequest, HttpServletResponse response) throws IOException {
        Invoice invoice = service.getById(invoicePdfReviewRequest.getId());
        if (invoice == null) {
            return;
        }
        if (StringUtils.isNotBlank(invoice.getSupplierId())) {
            Supplier sp = supplierService.getById(invoice.getSupplierId());
            BusinessException.throwErrorIf(ObjectUtil.isNull(sp), "供应商信息缺失，请检查");
            invoice.setSpInvoiceName(sp.getInvoiceName());
            invoice.setSpInvoiceNo(sp.getInvoiceNo());
            invoice.setSpInvoiceAddress(sp.getInvoiceAddress());
            invoice.setSpInvoiceBankName(sp.getInvoiceBankName());
            invoice.setSpInvoiceBankNo(sp.getInvoiceBankNo());
            invoice.setSpInvoiceTel(sp.getInvoiceTel());
        }
        if (StringUtils.isNotBlank(invoice.getHospitalId())) {
            Hospital hos = hospitalService.getById(invoice.getHospitalId());
            BusinessException.throwErrorIf(ObjectUtil.isNull(hos), "医院信息缺失，请检查");
            invoice.setHosInvoiceName(hos.getInvoiceName());
            invoice.setHosInvoiceNo(hos.getInvoiceNo());
            invoice.setHosInvoiceAddress(hos.getInvoiceAddress());
            invoice.setHosInvoiceBankName(hos.getInvoiceBankName());
            invoice.setHosInvoiceBankNo(hos.getInvoiceBankNo());
            invoice.setHosInvoiceTel(hos.getInvoiceTel());
        }

        //根据期号拼接成年月格式
        String periodNo = invoice.getPeriodNo();
        if (Strings.isNotBlank(periodNo)) {
            periodNo = periodNo.replaceAll("-", "").replaceAll("/", "");
            if (periodNo.length() > 5) {
                invoice.setPeriodNoDate(periodNo.substring(0, 4) + "年" + periodNo.substring(4, 6) + "月");
            }
        }
        PdfPageDirection pageDirection = PdfPageDirection.of(invoicePdfReviewRequest.getDirection());
        SpacingMode sMode = SpacingMode.of(invoicePdfReviewRequest.getSpacingMode());
        //查询医院是否配置自定义模板
        PrintTemplateVO iv = printTemplateService.findPrintTemplateVO(new PrintTemplateQueryRequest().setHospitalId(invoice.getHospitalId())
                .setBusType(invoicePdfReviewRequest.getBasePrintBusType().getValue()).setThisPageSize(invoicePdfReviewRequest.getPageSize()));
        //根据科室查询分组
        List<InvoiceItemVO> items = invoiceItemService.listForView(new InvoiceItemQueryRequest().setGroupByCondition(InvoiceGroupType.getByGroupType(invoice.getGroupByCondition())).setInvoiceId(invoicePdfReviewRequest.getId()));
        String templateJson = null;
        //如果配置自定义打印模板
        if (invoicePdfReviewRequest.getLoadFromTemplate() && iv.getId() != null) {
            //如果根据科室打印
            if (iv.getIsPrintDept() == 1) {
                //根据科室查询分组
                //items = invoiceItemService.listForView(new InvoiceItemQueryRequest().setGroupByCondition(invoice.getGroupByCondition()).setInvoiceId(id));
                // 数据分组；根据科室需要将数据进行分组
                Map<String, List<InvoiceItemVO>> invoiceMap = items.stream().collect(Collectors.groupingBy(o -> o.getDeptName()));
                List<PdfExportParams> exportParamslist = new ArrayList();
                //循环分组
                for (String key : invoiceMap.keySet()) {
                    templateJson = iv.getConfigJson();
                    //获取该组科室所有数据
                    List<InvoiceItemVO> invoiceItemVOS = invoiceMap.get(key);
                    //把科室set到 invoice
                    invoice.setDeptName(key);
                    //计算该科室的开票总金额
                    double totalAmount = invoiceItemVOS.stream().mapToDouble(o -> o.getSubtotalAmount()).sum();
                    invoice.setTotalAmount(totalAmount);
                    response.setContentType(MediaType.APPLICATION_PDF_VALUE);
                    response.addHeader("Content-Disposition", String.format("filename=%s.pdf", URLEncoder.encode("开票单-" + invoice.getCode())));
                    Map<String, Object> map = Maps.newHashMap();
                    map.put("invoice", invoice);
                    templateJson = FreeMarkers.renderString(templateJson, map);
                    PdfExportParams exportParams = PdfExportParams.fromJson(templateJson);
                    exportParams.setSpacingMode(sMode);
                    exportParams.setPageDirection(pageDirection);
                    exportParams.setPerRowsOfGrid(invoicePdfReviewRequest.getPerRowsOfGrid());
                    exportParams.setGridData(invoiceItemVOS);
                    exportParamslist.add(exportParams);
                }
                PdfExportUtils.exportMultiple(response.getOutputStream(), exportParamslist);
                return;

            } else {
                templateJson = iv.getConfigJson();
            }
        } else {
            templateJson = FileUtils.readClassPathFileToString(invoicePdfReviewRequest.getPrintPath(), "UTF-8");
        }
        response.setContentType(MediaType.APPLICATION_PDF_VALUE);
        response.addHeader("Content-Disposition", String.format("filename=%s.pdf", URLEncoder.encode("送货单-" + invoice.getCode())));
        Map<String, Object> map = Maps.newHashMap();
        map.put("invoice", invoice);
        Double totalNum = items.stream().mapToDouble(InvoiceItemVO::getQty).sum();
        invoice.setTotalNum(String.format("%.2f", totalNum));
        Double totalMoney = items.stream().mapToDouble(InvoiceItemVO::getSubtotalAmount).sum();
        invoice.setTotalMoney(String.format("%.2f", totalMoney));
        templateJson = FreeMarkers.renderString(templateJson, map);
        PdfExportParams exportParams = PdfExportParams.fromJson(templateJson);
        //打印时厂商列数据是英文时根据英文大写首字母换行
        items.forEach(item -> {
            item.setManufacturerName(getLongWord(item.getManufacturerName()));
            item.setLot(getLongWord(item.getLot()));
            item.setHasSunPurchaseCode(StringUtils.isBlank(item.getSunPurchaseCode()) ? "否" : "是");
            if (Strings.isNotBlank(item.getExpDate())) {
                try {
                    item.setExpDate(DateUtils.formatToAnotherPattern(item.getExpDate(),"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd"));
                } catch (ParseException e) {
                }
            }
        });
        exportParams.setSpacingMode(sMode);
        exportParams.setPageDirection(pageDirection);
        exportParams.setPerRowsOfGrid(invoicePdfReviewRequest.getPerRowsOfGrid());
        exportParams.setGridData(items);
        PdfExportUtils.export(response.getOutputStream(), exportParams);
    }

    private String getLongWord(String strWords) {
        if (StringUtils.isBlank(strWords)) {
            return "";
        }
        if (strWords.length() > 10) {
            String strNewWords = "";
            for (int i = 0; i < strWords.length(); i++) {
                char word = strWords.charAt(i);
                if (i != 0 && word == '/') {
                    strNewWords += " " + strWords.charAt(i);
                } else if (Character.isUpperCase(word)) { //如果为大写的，也加个空格
                    strNewWords += " " + strWords.charAt(i);
                } else {
                    strNewWords += strWords.charAt(i);
                }
            }
            return strNewWords;
        }
        return strWords;
    }


    /***
     * @Description:  批量删除开票明细
     */
    @DeleteMapping("/deleteBatchItem")
    public RestResult deleteBatchItem(@RequestBody Map<String, Object> para) {
        List<Serializable> ids = (List<Serializable>) para.get("ids");
        service.deleteBatchItem(ids);
        return RestResult.ok("操作成功");
    }


    /**
     * 生成开票单
     */
    @PostMapping("/buildInvoice")
    public CommonResult buildInvoice(@RequestBody @Valid InvoiceBuildDTO invoiceDTO) {
        return CommonResult.ok("开票成功", service.buildInvoice(invoiceDTO));
    }

    private void fillData(Invoice invoice) {
        if (StringUtils.isNotBlank(invoice.getSupplierId())) {
            Supplier supplier = supplierService.getById(invoice.getSupplierId());
            if (supplier != null) {
                invoice.setSpInvoiceName(supplier.getInvoiceName());
                invoice.setSpInvoiceNo(supplier.getInvoiceNo());
                invoice.setSpInvoiceAddress(supplier.getInvoiceAddress());
                invoice.setSpInvoiceBankName(supplier.getInvoiceBankName());
                invoice.setSpInvoiceBankNo(supplier.getInvoiceBankNo());
                invoice.setSpInvoiceTel(supplier.getInvoiceTel());
            }
        }
        if (StringUtils.isNotBlank(invoice.getHospitalId())) {
            Hospital hospital = hospitalService.getById(invoice.getHospitalId());
            if (hospital != null) {
                invoice.setHosInvoiceName(hospital.getInvoiceName());
                invoice.setHosInvoiceNo(hospital.getInvoiceNo());
                invoice.setHosInvoiceAddress(hospital.getInvoiceAddress());
                invoice.setHosInvoiceBankName(hospital.getInvoiceBankName());
                invoice.setHosInvoiceBankNo(hospital.getInvoiceBankNo());
                invoice.setHosInvoiceTel(hospital.getInvoiceTel());
            }
        }
    }


    /***
     * @param entity
     * @Description:  通用保存、更新
     */
    @PostMapping("/saveOrUpdateCommon")
    public RestResult saveOrUpdateCommon(@Valid @RequestBody Invoice entity) {
        boolean isOk = service.saveOrUpdate(entity);
        if (!isOk) {
            return RestResult.error("数据保存失败");
        }
        fillData(entity);
        return RestResult.ok("数据保存成功", entity);
    }

    /***
     * @param id
     * @Description:  通用查找，单个
     */
    @GetMapping("/findByIdCommon")
    public RestResult findByIdCommon(@RequestParam("id") Serializable id) {
        Invoice entity = service.getById(id);
        if (entity == null) {
            return RestResult.error("数据查询失败<br>可能所操作数据已被删除，请刷新后重新尝试操作");
        }
        fillData(entity);
        return RestResult.ok("获取数据成功", entity);
    }



    @Override
    public PageResult pageCommon(HttpServletRequest request, Invoice entity) {
        IPage<Invoice> page = QueryGenerator.parsePage(request);
        QueryWrapper<Invoice> queryWrapper = QueryGenerator.initQueryWrapper(request);
        fillQueryAuth(queryWrapper,entity);
        IPage pageResult = service.page(page, queryWrapper);

        //填充开票信息
        if (page.getTotal() > 0) {
            List<String> spidList = page.getRecords().stream().map(Invoice::getSupplierId).distinct().collect(Collectors.toList());
            List<String> hosidList = page.getRecords().stream().map(Invoice::getHospitalId).distinct().collect(Collectors.toList());

            Map<String,Supplier> spById = supplierService.list(Wrappers.<Supplier>query().in("id",spidList)).stream().collect(Collectors.toMap(Supplier::getId, item -> item));
            Map<String, Hospital> hosById = hospitalService.list(Wrappers.<Hospital>query().in("id",hosidList)).stream().collect(Collectors.toMap(Hospital::getId, item -> item));
            BusinessException.throwErrorIf(ObjectUtil.length(spidList) != ObjectUtil.length(spById.keySet()), "供应商信息缺失，请检查");
            BusinessException.throwErrorIf(ObjectUtil.length(hosidList) != ObjectUtil.length(hosById.keySet()), "医院信息缺失，请检查");
            for(Invoice invoice : page.getRecords()) {
                if (StringUtils.isNotBlank(invoice.getSupplierId())) {
                    invoice.setSpInvoiceName(spById.get(invoice.getSupplierId()).getInvoiceName());
                    invoice.setSpInvoiceNo(spById.get(invoice.getSupplierId()).getInvoiceNo());
                    invoice.setSpInvoiceAddress(spById.get(invoice.getSupplierId()).getInvoiceAddress());
                    invoice.setSpInvoiceBankName(spById.get(invoice.getSupplierId()).getInvoiceBankName());
                    invoice.setSpInvoiceBankNo(spById.get(invoice.getSupplierId()).getInvoiceBankNo());
                    invoice.setSpInvoiceTel(spById.get(invoice.getSupplierId()).getInvoiceTel());
                }
                if (StringUtils.isNotBlank(invoice.getHospitalId())) {
                    invoice.setHosInvoiceName(hosById.get(invoice.getHospitalId()).getInvoiceName());
                    invoice.setHosInvoiceNo(hosById.get(invoice.getHospitalId()).getInvoiceNo());
                    invoice.setHosInvoiceAddress(hosById.get(invoice.getHospitalId()).getInvoiceAddress());
                    invoice.setHosInvoiceBankName(hosById.get(invoice.getHospitalId()).getInvoiceBankName());
                    invoice.setHosInvoiceBankNo(hosById.get(invoice.getHospitalId()).getInvoiceBankNo());
                    invoice.setHosInvoiceTel(hosById.get(invoice.getHospitalId()).getInvoiceTel());
                }
            }
        }
        return PageResult.ok("查询成功", pageResult.getRecords(), pageResult.getTotal());
    }
}

